//
//  XMTabBaseViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/7.
//

#import "XMTabBaseViewController.h"
#import "XMHomeViewController.h"
#import "LoginPromptView.h"
#import "XMTabPubViewController.h"
#import "XMPublishViewController.h"
#import "XMLoginOtherPhoneController.h"
#import "XMLoginViewController.h"

@interface XMTabBaseViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) XMTabPubViewController *publishVC;
@property (nonatomic, strong) LoginPromptView *loginPromptView;
@property (nonatomic, assign) BOOL isLoggedIn; // 记录登录状态，初始为NO
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation XMTabBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始设置登录状态为未登录
      _isLoggedIn = NO;
    self.delegate = self;
      
      // 添加未登录提示View
//    [self setupLoginPromptView];
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)setupLoginPromptView {
    _loginPromptView = [LoginPromptView promptView];
    _loginPromptView.frame = CGRectMake(0, self.tabBar.frame.origin.y - _loginPromptView.bounds.size.height - self.view.safeAreaInsets.bottom - KScaleWidth(40), _loginPromptView.bounds.size.width, _loginPromptView.bounds.size.height);
    [self.view addSubview:_loginPromptView];
    _loginPromptView.hidden = _isLoggedIn;
}

// 模拟登录成功后的处理，实际开发中应根据登录接口返回情况修改登录状态
- (void)simulateLoginSuccess {
    _isLoggedIn = YES;
    _loginPromptView.hidden = YES;
}

- (void)setupTabBarTapListener {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabBarTapped:)];
    [self.tabBar addGestureRecognizer:tapGesture];
}

- (void)tabBarTapped:(UITapGestureRecognizer *)gesture {
    if (!_isLoggedIn) {
        _loginPromptView.hidden = NO;
    }
}

- (void)creatUI {
    self.tabBar.backgroundColor = kColorHEX(0xffffff);
    self.tabBar.tintColor = kColorHEX(0xff00ff);
    XMBaseNavigationController *homeNVC = [self iniWithClassName:@"XMHomeViewController" title:@"" imgStr:@"tabbar_home_icon" selectImgStr:@"tabbar_home_icon_select"];
    
    XMBaseNavigationController *recommendNVC = [self iniWithClassName:@"XMFindViewController" title:@"" imgStr:@"tabbar_recommend_icon" selectImgStr:@"tabbar_recommend_icon_select"];

    XMBaseNavigationController *publishNVC = [self iniWithClassName:@"XMTabPubViewController" title:@"" imgStr:@"tabbar_find_icon" selectImgStr:@"tabbar_find_icon_select"];
    _publishVC = [publishNVC.viewControllers firstObject];
    XMWeakSelf;
    _publishVC.saveSuccessBlock = ^{
        weakSelf.selectedIndex = weakSelf.currentIndex;
    };
    
    XMBaseNavigationController *messageNVC = [self iniWithClassName:@"XMMessageViewController" title:@"" imgStr:@"tabbar_msg_icon" selectImgStr:@"tabbar_message_icon_select"];
    
    XMBaseNavigationController *minNVC = [self iniWithClassName:@"XMMineViewController" title:@"" imgStr:@"tabbar_mine_icon" selectImgStr:@"tabbar_mine_icon_select"];
    
    self.viewControllers = @[homeNVC, recommendNVC, publishNVC, messageNVC, minNVC];
    
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(kScreenWidth/5*2, 0, kScreenWidth/5, kTabBarH) image:@"tabbar_publish_icon" selectImg:@"tabbar_publish_icon"];
    [publishBtn setImageEdgeInsets:UIEdgeInsetsMake(-kBottomLayout, 0, 0, 0)];
    [publishBtn addTarget:self action:@selector(publishAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:publishBtn];
}

- (XMBaseNavigationController *)iniWithClassName:(NSString *)className
                                           title:(NSString *)title
                                          imgStr:(NSString *)imgStr
                                    selectImgStr:(NSString *)selectImgStr {
    XMBaseViewController *baseVC = [[NSClassFromString(className) alloc] init];
    baseVC.tabBarItem.title = title;
    baseVC.tabBarItem.image = [ImageNamed(imgStr) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    baseVC.tabBarItem.selectedImage = [ImageNamed(selectImgStr) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [baseVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :  kColorHEX(0x4a4a4a)} forState:UIControlStateNormal];
    [baseVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : kMainColor} forState:UIControlStateSelected];
    
    XMBaseNavigationController *baseNVC = [[XMBaseNavigationController alloc] initWithRootViewController:baseVC];
    return baseNVC;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    XMBaseViewController *targetVC = [((XMBaseNavigationController *)viewController).viewControllers firstObject];
    if ([targetVC isKindOfClass:NSClassFromString(@"XMMessageViewController")] || [targetVC isKindOfClass:NSClassFromString(@"XMMineViewController")]) {
        if (userModel && userModel.accessToken.length > 0) {
            return YES;
        }
        else {
            [self login];
            return NO;
        }
    }
    return YES;
}

- (void)login {
    XMBaseViewController *currentVC = (XMBaseViewController *)[AppDelegate getCurrentVC];
//    XMLoginViewController *loginVC = [[XMLoginViewController alloc] init];
    XMLoginOtherPhoneController *loginVC = [[XMLoginOtherPhoneController alloc] init];
    XMBaseNavigationController *loginNVC = [[XMBaseNavigationController alloc] initWithRootViewController:loginVC];
    loginNVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [currentVC.navigationController presentViewController:loginNVC animated:YES completion:nil];
}

- (void)publishAction:(UIButton *)button {
    XMBaseViewController *currentVC = (XMBaseViewController *)[AppDelegate getCurrentVC];
    XMPublishViewController *publishVC = [[XMPublishViewController alloc] init];
    UINavigationController *publishNVC = [[UINavigationController alloc] initWithRootViewController:publishVC];
    publishNVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [currentVC presentViewController:publishNVC animated:YES completion:nil];
//    _currentIndex = self.selectedIndex;
//    self.selectedIndex = 2;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
